package com.mck.config;

// import com.mck.interceptor.OperationLogInterceptor;
import com.mck.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    // @Autowired
    // private OperationLogInterceptor operationLogInterceptor;

    @Autowired
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/**", "/static/**", "/favicon.ico")
                .addResourceLocations("classpath:/static/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("Authorization");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // // 先添加Token拦截器
        // registry.addInterceptor((HandlerInterceptor) tokenInterceptor)
        // .addPathPatterns("/api/**") // 拦截api请求
        // .excludePathPatterns("/static/**", "/favicon.ico"); // 排除静态资源请求和favicon.ico请求

        // // 再添加操作日志拦截器
        // registry.addInterceptor(operationLogInterceptor)
        // .addPathPatterns("/**")
        // .excludePathPatterns("/api/operation-logs/**");
    }

    // 配置异步请求处理
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(300000); // 5分钟超时
        configurer.setTaskExecutor(new SimpleAsyncTaskExecutor());
    }
}
